package com.app.util.file;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

import com.app.util.type.LongUtil;


public class FileFingerMark {

	/**
	 * 得到文件的指纹
	 * 
	 * @param file
	 * @return
	 * @throws IOException
	 */
	public static byte[][] get(File file) throws IOException {
		byte[][] bss;
		final int fingerMarkCount = 32;
		final int perFingerMarkSum = 128;
		RandomAccessFile raf;
		raf = new RandomAccessFile(file, "r");
		try {
			if (file.length() <= fingerMarkCount - 1) {
				bss = new byte[2][];
				bss[0] = LongUtil.toBytes(file.length());
				bss[1] = new byte[(int) file.length()];
				raf.read(bss[1]);
				return bss;
			} else {
				bss = new byte[fingerMarkCount][];
				long num = file.length() / (fingerMarkCount - 1);
				bss[0] = LongUtil.toBytes(file.length());
				for (int i = 1; i < fingerMarkCount; i++) {
					if ((i - 1) * num >= file.length()) {
						break;
					}
					raf.seek(i * num);
					long size = file.length() - (i * num);
					if (size > perFingerMarkSum)
						size = perFingerMarkSum;
					bss[i] = new byte[(int) size];
					raf.read(bss[i]);
				}
				return bss;
			}
		} finally {
			raf.close();
		}
	} 
}
